import "./public-path";
import Vue from "vue";
import VueRouter from "vue-router";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import App from "./App.vue";
import { routers } from "./router";
import axios from "axios";

let router = null;
let instance = null;
let history = null;
Vue.use(VueRouter);
Vue.use(ElementUI);

let originFn = document.body.appendChild.bind(document.body);

async function render(props = {}) {
  const { container, microWidgetProps } = props;

  Vue.prototype.$microWidgetProps = microWidgetProps;
  // 获取当前host，兼容客户端
  const host = microWidgetProps.config.systemInfo.realLocation.origin;
  const token = microWidgetProps.token.getToken.access_token;

  Vue.prototype.axios = axios.create({
    baseURL: host,
    headers: {
      Authorization: `Bearer ${token}`,
    },
  });

  redirectPopup(container);

  
  let get_user_res = await Vue.prototype.axios
  .post(`/api/eacp/v1/user/get`)
  .then((get_user_res) => [null, get_user_res])
  .catch((err) => [err, null]);

  if (!get_user_res[1]) {
    // microWidgetProps.components.toast.error(JSON.stringify(name_path[1]));
    this.$microWidgetProps.components.toast.error(JSON.stringify(get_user_res[1]));
    return false;
  }

  get_user_res = get_user_res[1];

  console.log("get_user_res.data.ismanager");
  console.log(get_user_res.data.ismanager);

  if(!get_user_res.data.ismanager){
    console.log("get_user_res.data.ismanager");
    console.log(get_user_res.data.ismanager);
    microWidgetProps.components.toast.error("只有组织管理员才能使用此功能！");
    return false;
  }

  console.log("get_user_res");
  console.log(get_user_res);

  let base = window.__POWERED_BY_QIANKUN__
    ? microWidgetProps.history.getBasePath.split("#")[0] + "/"
    : "/";
  // console.log(microWidgetProps.contextMenu.dirDocid);
  router = new VueRouter({
    base,
    routes: routers,
    mode: "hash",
  });
  // console.log(router, "router");
  instance = new Vue({
    router,
    render: (h) => h(App),
  }).$mount(container ? container.querySelector("#app") : "#app");
}

function redirectPopup(container) {
  const addPopup = "el-select-dropdown el-popper";
  // const editPopup = 'el-message-box v-modal'
  const whiteList = [addPopup];

  let originFn = document.body.appendChild.bind(document.body);

  document.body.appendChild = (dom) => {
    if (whiteList.includes(dom.className)) {
      try {
        container.querySelector("#app").appendChild(dom);
      } catch (error) {
        console.log(JSON.stringify(error));
      }
    } else {
      originFn(dom);
    }
  };
}

if (!window.__POWERED_BY_QIANKUN__) {
  render();
}

export async function bootstrap() {
  // console.log("%c ", "color: green;", "vue3.0 app bootstraped");
}

function storeTest(props) {
  props.onGlobalStateChange &&
    props.onGlobalStateChange(
      (value, prev) =>
        console.log(`[onGlobalStateChange - ${props.name}]:`, value, prev),
      true
    );
  props.setGlobalState &&
    props.setGlobalState({
      ignore: props.name,
      user: {
        name: props.name,
      },
    });
}

export async function mount(props) {
  storeTest(props);
  render(props);
}

export async function unmount() {
  instance?.unmount?.();
  instance = null;
  router = null;
  history?.destroy?.();
  document.body.appendChild = originFn;
}